﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.Parameters
{
    public partial class TimeSection : Base.BasePage
    {
        ExtendBLL.TimeSection ET = new ExtendBLL.TimeSection();  
        LabMS.Model.CourseTiming MCT = new LabMS.Model.CourseTiming();

        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("JCSJDZ");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                InitBind(1);//默认为夏季
            }
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void InitBind(int Type)
        {
            DataSet ds = new DataSet();
            string str = "季节="+Type;
            ds = ET.GetList(str);

            for (int i = 1; i <= 12; i++)
            {
                HtmlInputText text1 = (HtmlInputText)this.FindControl("a" + i.ToString());
                HtmlInputText text2 = (HtmlInputText)this.FindControl("b" + i.ToString());
                HtmlInputText text3 = (HtmlInputText)this.FindControl("c" + i.ToString());
                HtmlInputText text4 = (HtmlInputText)this.FindControl("d" + i.ToString());

                ddlSys.SelectedValue = ds.Tables[0].Rows[i-1]["季节"].ToString();

                DateTime temp = new DateTime();
                if (!DateTime.TryParse(ds.Tables[0].Rows[i - 1]["STime"].ToString(), out temp))//一种传参方式，可以在外面使用temp
                {
                    temp = DateTime.Now;//转换失败，取当前时间
                }
                string T1= temp.ToShortTimeString();
                text1.Value = T1.Substring(0, T1.IndexOf(":"));//8:00
                text2.Value = T1.Substring(T1.IndexOf(":") + 1,2) == "00" ? "00" : T1.Substring(T1.IndexOf(":") + 1, 2);

                DateTime temp2 = new DateTime();
                if (!DateTime.TryParse(ds.Tables[0].Rows[i - 1]["ETime"].ToString(), out temp2))
                {
                    temp2 = DateTime.Now;
                }
                string T2= temp2.ToShortTimeString();
                text3.Value=T2.Substring(0,T2.IndexOf(":"));
                text4.Value = T2.Substring(T2.IndexOf(":") + 1, 2) == "00" ? "00" : T2.Substring(T2.IndexOf(":") + 1, 2);
                //string str3 = "8:10:00";
                //string str4 = str3.Substring(str3.IndexOf(":")+1, str3.LastIndexOf(":") - str3.IndexOf(":"));
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool success = false;
            for (int i = 1; i <= 12; i++)
            {
                HtmlInputText text1 = (HtmlInputText)this.FindControl("a"+i.ToString());
                HtmlInputText text2 = (HtmlInputText)this.FindControl("b" + i.ToString());
                HtmlInputText text3 = (HtmlInputText)this.FindControl("c" + i.ToString());
                HtmlInputText text4 = (HtmlInputText)this.FindControl("d" + i.ToString());
                MCT.Nub = i;
                if (ddlSys.SelectedValue == "1")//夏季,规定数据库中前12项存夏季时间，后12项存冬季时间
                {
                    MCT.ID = i;
                }
                else
                {
                    MCT.ID =i + 12;
                }
                MCT.季节 =int.Parse(ddlSys.SelectedValue.ToString());

                DateTime temp = new DateTime();
                if (!DateTime.TryParse(text1.Value + ":" + text2.Value, out temp))//一种传参方式，可以在外面使用temp
                {
                    temp = DateTime.Now;//转换失败，取当前时间
                }
                MCT.STime=DateTime.Parse(temp.ToShortTimeString());

                DateTime temp2 = new DateTime();
                if (!DateTime.TryParse(text3.Value + ":" + text4.Value, out temp2))
                {
                    temp2 = DateTime.Now;
                }
                MCT.ETime =DateTime.Parse(temp2.ToShortTimeString());//2009-3-10 7:00

                //MCT.STime = DateTime.Parse(text1.Value + ":" + text2.Value); // 开始时间
                //MCT.ETime =DateTime.Parse( text3.Value +":"+ text4.Value);//结束时间
                try
                {
                    ET.Update(MCT);
                    success = true;
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }
            if (success == true)
            {
                Common.JShelper.JSAlert(this.Page, "", "更新成功！");
                InitBind(int.Parse(ddlSys.SelectedValue.ToString()));
            }
            else
            {
                Common.JShelper.JSAlert(this.Page, "", "更新失败！");
            }
        }

        protected void ddlSys_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlSys.SelectedValue == "1")//夏季
            {
                InitBind(1);
            }
            else//冬季
            {
                InitBind(2);
            }
        }
    }
}
